import "./login.css"
import { useHistory, } from "react-router-dom"
import { Card, Form, Input, Button, Checkbox, message, } from 'antd';
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import imgicon from "./IMG/盒马.icon.jfif"
// 引入用户的api
import userApi from "../../api/users"


// eslint-disable-next-line
export default () => {
  let history = useHistory()
  // const params=useParams()

  const onFinish = async (values) => {
    const { username, password } = values
    const data = await userApi.login({ username, password })
    if (data.status) {
    userApi.setUserId(data.info._id)
      await userApi.setToken(data.token)

      message.success({
        content: "登录成功，即将跳转", duration: 1, onClose: () => {
          history.push("/info/seckill_list")
        }
      })
      userApi.setToken(data.token)
    } else {
      message.error({ content: "登陆失败，请重新登录", duration: 1 })
    }
  }



  return (
    <div className="site-card-border-less-wrapper" id="components-form-demo-normal-login" style={{
      display: "flex", justifyContent: "center"
    }}>
      <Card bordered={false} style={{ width: 400, marginTop: "15vh" }}>
        <Form.Item style={{ margin: "auto", width: 50, marginBottom: 15 }}>
          <Card.Meta
            avatar={
              <img src={imgicon} alt="" style={{ width: 50, height: 50 }} />
            }
          />
        </Form.Item>
        <Form
          name="normal_login"
          className="login-form"
          initialValues={{
            remember: true,
          }}
          onFinish={onFinish}
        >
          <Form.Item
            name="username"
            rules={[
              {
                required: true,
                message: '请输入用户名',
              },
            ]}
          >
            <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="请输入用户名" />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[
              {
                required: true,
                message: '请输入密码',
              },
            ]}
          >
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="请输入密码"
            />
          </Form.Item>
          <Form.Item>
            <Form.Item name="remember" valuePropName="checked" noStyle>
              <Checkbox>记住密码</Checkbox>
            </Form.Item>
            <a className="login-form-forgot" href="\">
              忘记密码
            </a>
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit" className="login-form-button">
              登录
            </Button>
            <div style={{ height: "20px" }}></div>
            <a href="#/reg">立即注册</a>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}